﻿using System.Linq;
using MicroMVVM.Services.Interfaces;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;

namespace MicroMVVM.Services.Implementation.Any
{
    [ExportService(ServiceType.Any, typeof (IToastMessageService), PartCreationPolicy.Singleton)]
    public class ToastMessageService : IToastMessageService
    {
        /// <summary>
        /// Shows the toast message with template type ToastText01.
        /// http://msdn.microsoft.com/en-gb/library/windows/apps/hh761494.aspx"
        /// </summary>
        /// <param name="message">Toast message hello</param>
        public void ShowToastText01(string message)
        {
            var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

            ShowToastMessage(xml, null, null, new[] {message});
        }

        /// <summary>
        /// Shows the toast message with template type ToastText02.
        /// http://msdn.microsoft.com/en-gb/library/windows/apps/hh761494.aspx"
        /// </summary>
        /// <param name="headline">Toast headline</param>
        /// <param name="message">Toast message</param>
        public void ShowToastText02(string headline, string message)
        {
            var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

            ShowToastMessage(xml, null, null, new[] {headline, message});
        }

        /// <summary>
        /// Shows the toast message with template type ToastText03.
        /// http://msdn.microsoft.com/en-gb/library/windows/apps/hh761494.aspx"
        /// </summary>
        /// <param name="headline">Toast headline</param>
        /// <param name="message">Toast message</param>
        public void ShowToastText03(string headline, string message)
        {
            var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText03);

            ShowToastMessage(xml, null, null, new[] {headline, message});
        }

        /// <summary>
        /// Shows the toast message with template type ToastText04.
        /// http://msdn.microsoft.com/en-gb/library/windows/apps/hh761494.aspx"
        /// </summary>
        /// <param name="headline">Toast headline</param>
        /// <param name="message1">Toast first message</param>
        /// <param name="message2">Toast second message</param>
        public void ShowToastText04(string headline, string message1, string message2)
        {
            var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText04);

            ShowToastMessage(xml, null, null, new[] {headline, message1, message2});
        }

        /// <summary>
        /// Shows the toast message with template type ToastImageAndText01.
        /// http://msdn.microsoft.com/en-gb/library/windows/apps/hh761494.aspx"
        /// </summary>
        /// <param name="imageSource">Toast image source</param>
        /// <param name="message">Toast message</param>
        /// <param name="alt">Toast image alternate text</param>
        public void ShowToastImageAndText01(string imageSource, string message, string alt = "")
        {
            var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01);

            ShowToastMessage(xml, imageSource, alt, new[] {message});
        }

        /// <summary>
        /// Shows the toast message with template type ToastImageAndText02.
        /// http://msdn.microsoft.com/en-gb/library/windows/apps/hh761494.aspx"
        /// </summary>
        /// <param name="imageSource">Toast image source</param>
        /// <param name="headline">Toast headline</param>
        /// <param name="message">Toast message</param>
        /// <param name="alt">Toast image alternate text</param>
        public void ShowToastImageAndText02(string imageSource, string headline, string message, string alt = "")
        {
            var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText02);

            ShowToastMessage(xml, imageSource, alt, new[] {headline, message});
        }

        /// <summary>
        /// Shows the toast message with template type ToastImageAndText03.
        /// http://msdn.microsoft.com/en-gb/library/windows/apps/hh761494.aspx"
        /// </summary>
        /// <param name="imageSource">Toast image source</param>
        /// <param name="headline">Toast headline</param>
        /// <param name="message">Toast message</param>
        /// <param name="alt">Toast image alternate text</param>
        public void ShowToastImageAndText03(string imageSource, string headline, string message, string alt = "")
        {
            var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText03);

            ShowToastMessage(xml, imageSource, alt, new[] {headline, message});
        }

        /// <summary>
        /// Shows the toast message with template type ToastImageAndText04.
        /// http://msdn.microsoft.com/en-gb/library/windows/apps/hh761494.aspx"
        /// </summary>
        /// <param name="imageSource">Toast image source</param>
        /// <param name="headline">Toast headline</param>
        /// <param name="message1">Toast first message</param>
        /// <param name="message2">Toast second message</param>
        /// <param name="alt">Toast image alternate text</param>
        public void ShowToastImageAndText04(string imageSource, string headline, string message1, string message2,
                                            string alt = "")
        {
            var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);

            ShowToastMessage(xml, imageSource, alt, new[] {headline, message1, message2});
        }

        /// <summary>
        /// Shows the toast message of the specified template type.
        /// </summary>
        /// <param name="xml">Toast Template Type</param>
        /// <param name="imageSource">Image Source</param>
        /// <param name="alt">Toast image alternate text</param>
        /// <param name="messages">List of messages</param>
        private static void ShowToastMessage(XmlDocument xml, string imageSource, string alt, params string[] messages)
        {
            if ((messages != null) && (messages.Any()))
            {
                var index = 0;

                var textNodes = xml.GetElementsByTagName("text");

                foreach (var message in messages)
                {
                    var meassageNode = textNodes.ElementAtOrDefault(index);

                    if (meassageNode != null) meassageNode.AppendChild(xml.CreateTextNode(message));

                    index++;
                }
            }

            imageSource = (imageSource == null) ? null : imageSource.Trim();

            if (string.IsNullOrEmpty(imageSource) == false)
            {
                var imageNodes = xml.GetElementsByTagName("image");

                var imageNode = imageNodes.ElementAtOrDefault(0);

                if (imageNode != null)
                {
                    ((XmlElement) imageNode).SetAttribute("src", imageSource);

                    if (string.IsNullOrEmpty(alt) == false) ((XmlElement) imageNode).SetAttribute("alt", alt);
                }
            }

            var toast = new ToastNotification(xml);

            ToastNotificationManager.CreateToastNotifier().Show(toast);
        }
    }
}